package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.enums.Coord;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.HwndUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 难度副本
 */
@Slf4j
public class InstanceHardTaskImpl extends AbstractTask {

    private final int number;

    public InstanceHardTaskImpl(WinDef.HWND hwnd_0, int number) {
        super(hwnd_0);
        this.number = number;
    }

    // 二重影
    private final List<String> CONFIRM_BATTLE_PICS_ECY = Arrays.asList(
            PicConstants.侠士副本_二重影_确认战斗1,
            PicConstants.侠士副本_二重影_确认战斗2,
            PicConstants.侠士副本_二重影_确认战斗3
    );
    // 万丈山
    private final List<String> CONFIRM_BATTLE_PICS_WZS = Arrays.asList(
            PicConstants.侠士副本_万丈山_确认战斗1,
            PicConstants.侠士副本_万丈山_确认战斗2,
            PicConstants.侠士副本_万丈山_确认战斗3
    );

    @Override
    public Boolean before() throws Exception {
        clickEsc1();
        return goToChangAn();
    }

    @Override
    public Boolean execute() throws Exception {
        // 打开长安小地图
        MouseUtil.lClickRandom(hwnd_0, Coord.CURRENT_MAP_XY.getX(),  Coord.CURRENT_MAP_XY.getY());
        ThreadUtil.sleep1();
        MouseUtil.lClickRandom(hwnd_0, Coord.BAI_XIAO_XIAN_ZI_XY.getX(),  Coord.BAI_XIAO_XIAN_ZI_XY.getY());

        // 判断是否要点击”放大镜“
//        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.普通副本_任务图标_1s, 2).isFinded()) {
//            // 点击”放大镜“
//            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.普通副本_任务图标_1, 2)) {
//                return false;
//            }
//        }
//
//        for (int i = 0; i < 20; i++) {
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.普通副本_任务图标_2, 1)) {
//                break;
//            }
//            // 如果没找到下拉滑动屏幕寻找图标
//            MouseUtil.dragMove(hwnd_0, Coord.INSTANCE_SELECT.getX(), Coord.INSTANCE_SELECT.getY(), 0, -80);
//            ThreadUtil.sleep1();
//        }

        ThreadUtil.sleep(5000);

        // 查找并点击“选择副本”按钮
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.普通副本_选择副本按钮, 5)) {
            log.error("【{}】查找并点击“选择副本”按钮失败！", getTaskName());
            return false;
        }
        // 查找并点击“侠士副本”按钮
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.侠士副本_选择侠士副本, 5)) {
            log.error("【{}】查找并点击“侠士副本”按钮失败！", getTaskName());
            return false;
        }

        if (number == 1) {
            MouseUtil.lClickRandom(hwnd_0, Coord.DUNGEON_NORMAL_JR1_DXY.getX(), Coord.DUNGEON_NORMAL_JR1_DXY.getY());
        } else {
            MouseUtil.lClickRandom(hwnd_0, Coord.DUNGEON_NORMAL_JR2_DXY.getX(), Coord.DUNGEON_NORMAL_JR2_DXY.getY());
        }
        ThreadUtil.sleep3();

        // 进入副本，队友需要点准备
        teamReady();
        return true;
    }

    @Override
    public Boolean after() throws Exception {
        ThreadUtil.sleep3();
        return runInstance();
    }

    @Override
    public String getTaskName() throws Exception {
        return "难度副本" + number;
    }

    private boolean switchInstance() throws Exception {
        // 二重影 50级
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.普通副本_二重影_判断副本, 3).isFinded()) {
            log.info("进入“二重影”难度副本...");
            return runInstance(CONFIRM_BATTLE_PICS_ECY);
        }
        // 万丈山 70级
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.普通副本_万丈山_判断副本, 3).isFinded()) {
            log.info("进入“万丈山”难度副本...");
            return runInstance(CONFIRM_BATTLE_PICS_WZS);
        }
        return false;
    }

    /**
     * 队友准备
     */
    private void teamReady() throws Exception {
        for (Map.Entry<String, HwndUtil.HwndEntity> hwndEntity : HwndUtil.hwndsMap.entrySet()) {
            // 防止关闭其他窗口导致广播报错
            try {
                // 准备按钮会闪烁
                if (FuzzyPicUtil.findAndClickRandom(hwndEntity.getValue().getHwnd(), PicConstants.侠士副本_准备, 2, 0.8)) {
                    log.info("【{}】句柄 {} 成功点击准备", getTaskName(), hwndEntity.getKey());
                }
            } catch (Exception e) {
            }
        }
    }
}
